#include "thread_pool.h"
#include "user.h"

//主线程调用:处理客户端发过来的消息
void handleMessage(int sockfd, int epfd, task_queue_t * que)
{
    //消息格式：cmd content
    //1.1 获取消息长度
    int length = -1;
    int ret = recvn(sockfd, &length, sizeof(length));
    printf("recv  length: %d\n", length);

    //1.2 获取消息类型
    int cmdType = -1;
    ret = recvn(sockfd, &cmdType, sizeof(cmdType));
    printf("recv cmd type: %d\n", cmdType);
    task_t *ptask = (task_t*)calloc(1, sizeof(task_t));
    ptask->peerfd = sockfd;
    ptask->epfd = epfd;
    ptask->type=(CmdType)cmdType;
    if (cmdType == CMD_TYPE_PUTS) {
        //如过是上传任务，再接触监听，避免水平触发，导致多个线程同时执行
        delEpollReadfd(epfd, sockfd);
    }
    if(length > 0) {
        //1.3 获取消息内容
        ret = recvn(sockfd, ptask->data, length);
        if(ret > 0) {
            //往线程池中添加任务
            taskEnque(que, ptask);
        }
    } else if(length == 0){
        taskEnque(que, ptask);
    }

    if(ret == 0) {//连接断开的情况
        printf("\nconn %d is closed.\n", sockfd);
        delEpollReadfd(epfd, sockfd);
        close(sockfd);
    }
}


//注意：此函数可以根据实际的业务逻辑，进行相应的扩展
void doTask(task_t * task)
{
    assert(task);
    char log_info[128];

    switch(task->type) {
    case CMD_TYPE_PWD:
        snprintf(log_info, sizeof(log_info), "pwd %s", task->data);
        logMessage(log_info);
        pwdCommand(task);   break;

    case CMD_TYPE_CD:
        snprintf(log_info, sizeof(log_info), "cd %s", task->data);
        logMessage(log_info);   
        cdCommand(task);    break;

    case CMD_TYPE_LS:
        snprintf(log_info, sizeof(log_info), "ls %s", task->data);
        logMessage(log_info);
        lsCommand(task);    break;

    case CMD_TYPE_MKDIR:
        snprintf(log_info, sizeof(log_info), "mkdir %s", task->data);
        logMessage(log_info);
        mkdirCommand(task);  break;

    case CMD_TYPE_RMDIR:
        snprintf(log_info, sizeof(log_info), "rmdir %s", task->data);
        logMessage(log_info);
        rmdirCommand(task);  break;

    case CMD_TYPE_NOTCMD:
        snprintf(log_info, sizeof(log_info), "notcmd %s", task->data);
        logMessage(log_info);
        notCommand(task);   break;

    case CMD_TYPE_PUTS:
        snprintf(log_info, sizeof(log_info), "puts %s", task->data);
        logMessage(log_info);
        puts("1");
        putsCommand(task);   
        addEpollReadfd(task->epfd, task->peerfd);
        break;
    case CMD_TYPE_TREE:
        snprintf(log_info, sizeof(log_info), "tree %s", task->data);
        logMessage(log_info);
        treeCommand(task);   break;
    case CMD_TYPE_QUIT:
        snprintf(log_info, sizeof(log_info), "quit %s", task->data);
        logMessage(log_info);
        quitCommand(task);   break;
    case CMD_TYPE_GETS:
        getsCommand(task);    break;
    case CMD_TYPE_REGISTER1:
        register1Command(task);    break;
    case CMD_TYPE_REGISTER2:
        register2Command(task);    break;
    case CMD_TYPE_LOGIN1:
        userlogin1Command(task);    break;
    case CMD_TYPE_LOGIN2:
        userlogin2Command(task);    break;
    }
}

//每一个具体任务的执行，交给一个成员来实现
void notCommand(task_t * task){

}
